package com.xiang.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * es配置
 *
 * @author xiangtianlei@gogpay.cn
 * @date 2023-01-29
 */
public class EsConfig {
    public static final String INDEX_NAME = "customer";
    public static final String TYPE = "_doc";

    /**
     * 得到RestHighLevelClient
     *
     * @return {@link RestHighLevelClient }
     */
    public static RestHighLevelClient getClient() {
        //  创建httpHost对象
        HttpHost httpHost = new HttpHost("localhost", 9200);
        // 创建RestClientBuilder
        RestClientBuilder clientBuilder = RestClient.builder(httpHost);
        // 创建RestHighLevelClient
        return new RestHighLevelClient(clientBuilder);
    }

    /**
     * 得到mapping
     *
     * @return {@link String }
     */
    public static String getMapping() {
        return "{\n" +
                "  \"properties\":{\n" +
                "    \"name\":{\n" +
                "      \"type\":\"keyword\"\n" +
                "    },\n" +
                "    \"age\":{\n" +
                "      \"type\":\"long\"\n" +
                "    },\n" +
                "    \"sex\":{\n" +
                "      \"type\":\"keyword\"\n" +
                "    },\n" +
                "    \"address\":{\n" +
                "      \"type\":\"text\"\n" +
                "    },\n" +
                "    \"createTime\":{\n" +
                "      \"type\":\"date\",\n" +
                "      \"format\":\"yyyy-MM-dd HH:mm:ss\"\n" +
                "    }\n" +
                "  }\n" +
                "}";
    }
}
